package com.chaoyi.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.Fiction;
import com.chaoyi.service.FictionService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;

@Controller
@MultipartConfig
@RequestMapping("fiction")
public class FictionController {
	@Resource
	FictionService finctionService;
	
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mod =new ModelAndView("/fiction/fictionList");
		return mod;
	}
	/**
	 * 首页展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("fictionList")
	@ResponseBody
	public JSONObject fictionList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<Fiction> list = finctionService.findList(offset, limit);
		int count =finctionService.findCount();
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	/**
	 *  新增首页内容跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addFiction_jump")
	public ModelAndView addFiction_jump(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/fiction/addFiction");
		return mod;
	}
	/**
	 * 保存新增首页内容
	 */
	@ResponseBody
	@RequestMapping("addFiction")
	public JSONObject addFiction(HttpServletRequest request, HttpServletResponse response, Fiction fiction) {
		JSONObject json = new JSONObject();
		int number = finctionService.saveFiction(fiction);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改首页内容
	 */
	@RequestMapping("/alertFiction")
	public ModelAndView alertFiction(HttpServletRequest request, HttpServletResponse response,int id) {
		ModelAndView mod = new ModelAndView("/fiction/alertFiction");
		Fiction fiction = finctionService.findFictionOne(id);
		mod.addObject("obj",fiction);
		return mod;
	}
	
	/**
	 * 保存修改的私播信息
	 */
	@ResponseBody
	@RequestMapping("alertFictionSave")
	public JSONObject alertFictionSave(HttpServletRequest request, HttpServletResponse response, Fiction fiction) {
		JSONObject json = new JSONObject();
		int number = finctionService.alertFictionSave(fiction);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 删除首页内容
	 */
	@ResponseBody
	@RequestMapping("deleteFiction")
	public JSONObject deleteFiction(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = finctionService.deleteFiction(id);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	/**
	 * 新增首页封面上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyImg")
	@ResponseBody
	public JSONObject uploadClassifyImg(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String imgUrl = null;
		try{
			imgUrl = finctionService.uploadOSS(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(imgUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", imgUrl).toJSON();
		return result;
	}
	/**
	 * 新增首页视频上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadOSS")
	@ResponseBody
	public JSONObject uploadOSS(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String fileUrl = null;
		try{
			fileUrl = finctionService.uploadOSS(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(fileUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", fileUrl).toJSON();
		return result;
	}
}
